<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.translation.dao.Selector"%>
<%@page import="com.translation.dao.Batch"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.translation.dao.Batch"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="com.translation.dao.BaseDAO"%>
<%@page import="com.translation.util.Util"%>
<%@page import="com.translation.entity.Phrase"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.io.IOException"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Verb Edit</title>
</head>
<%!
private BaseDAO dao = new BaseDAO();
%>
<body>
<%
        final String phrase_id = request.getParameter("phrase_id");
        final String locale_id = Util.toNull((String) session.getAttribute("locale_id"));
        final String submit = request.getParameter("submit");
        final Phrase phrase = new Phrase();
        request.setAttribute("phrase",phrase);
        
        if (phrase_id != null) {

            Selector selector = new Selector() {
                public void setParams(PreparedStatement st) throws SQLException,IOException {
                    int index = 1;
                    st.setString(index++, phrase_id);
                }
                
                public void row(ResultSet rs) throws SQLException,IOException {
                	phrase.phrase = rs.getString("phrase");
                	phrase.locale_id = rs.getString("locale_id");
                	phrase.freq = rs.getString("freq");
                }
            };
            dao.selectQuery("phrase_byid.sql", selector);
        }
        
        if (submit != null) {
            final HttpServletRequest req = request;
            
            Batch batch = new Batch() {
                @Override
                public void setParams(PreparedStatement st) throws SQLException,IOException {
                    int index = 1;
                    st.setString(index++,req.getParameter("phrase"));
                    st.setString(index++,req.getParameter("locale_id"));
                    if (phrase_id != null){
                        st.setString(index++,phrase_id);
                    }
                }

                @Override
                public boolean result(int[] result, PreparedStatement st) throws SQLException,IOException {
                    return result != null && result.length==1;
                }

                @Override
                public boolean next() throws SQLException {
                    return true;
                }

                @Override
                public int getSize() {
                    return 1;
                }

                @Override
                public void error(SQLException e) {
                    e.printStackTrace();
                }                
            };
            dao.namedBatch((phrase_id != null)? "word_save": "word_insert", batch);
        }
%>
    <form action="verb_edit.jsp">
        <table>
            <tr>
                <td>Word Id: </td>
                <td>
                    <input type="text" size="40" name="phrase_id" value="${phrase.phrase_id}"/>
                </td>
            </tr>
            <tr>
                <td>Word: </td>
                <td>
                    <input type="text" size="40" name="phrase" value="${phrase.phrase}"/>
                </td>
            </tr>
            <tr>
                <td>Locale: </td>
                <td>
                    <select name="locale_id">
                        <option></option>
                        <option value="es"<%= ("es".equals(phrase.locale_id)? "selected=\"selected\"":"") %>>ES</option>
                        <option value="fr"<%= ("fr".equals(phrase.locale_id)? "selected=\"selected\"":"") %>>FR</option>
                        <option value="de" <%= ("de".equals(phrase.locale_id)? "selected=\"selected\"":"") %>>DE</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="submit" value="Save" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>